package cn.itwangxiang.android.ui;

import cn.itwangxiang.android.AppContext;
import cn.itwangxiang.android.R;
import cn.itwangxiang.android.common.SQLiteUtils;
import cn.itwangxiang.android.common.StringUtils;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DemoStorage extends Activity {
	private AppContext appContext;
	
	private SQLiteUtils sqLiteUtils;

	private TextView textView;
	
	private EditText editText;
	
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.demo_storage);

		appContext = (AppContext) getApplication();
		sqLiteUtils = appContext.sqLiteUtils;
		
		textView = (TextView) findViewById(R.id.textView1);
		String textVal = "";
		
		editText = (EditText) findViewById(R.id.editText1);
		
		button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if("".equals(editText.getText().toString())){
					Toast.makeText(DemoStorage.this, "请填写要修改的用户名称", Toast.LENGTH_SHORT).show();
				}else{
					SQLiteDatabase dbw = sqLiteUtils.getWritableDatabase();
					
					ContentValues cv=new ContentValues();
	                cv.put("name", editText.getText().toString());
	                /*
	                 *    第一个参数是：表名
	                 *    第二个参数ContentValues对象
	                 *    第三个参数where子句，里面的问题是一个占位符
	                 *    第四个参数是占位符对应的值 
	                 *    注意：有几个占位符第四个参数里面就应该有几个值
	                 */
	                dbw.update("person", cv, "id like '%' ", null);
	                dbw.close();
				}
				getUserDate();
			}
		});
		getUserDate();
	}

	private void getUserDate() {
		String textVal ="";
		SQLiteDatabase db = sqLiteUtils.getReadableDatabase();

		Cursor result = db.rawQuery("SELECT ID, name FROM person", null);
		
		result.moveToFirst();
		while (!result.isAfterLast()) {
			int id = result.getInt(0);
			String name = result.getString(1);
			textVal +="id:"+id+";name:"+name+";";
			result.moveToNext();
		}
		result.close();
		
		textView.setText(textVal);
	}
}
